package org.jiuling.aliyunmarket.moji.util;

import org.jiuling.aliyunmarket.config.AliyunMarketConfig;
import org.jiuling.aliyunmarket.moji.enums.ApiEnums;
import org.jiuling.local.tools.exception.LocalToolsException;
import org.jiuling.local.tools.random.RandomValue;
import org.jiuling.local.tools.verify.Assertions;
import org.jiuling.network.tools.http.request.HttpRequest;
import org.jiuling.network.tools.http.request.enums.RequestMethodEnum;
import org.jiuling.network.tools.http.request.exception.RequestException;
import org.jiuling.network.tools.http.request.factory.HttpRequestFactory;
import org.jiuling.network.tools.http.request.model.HttpRequestHeader;
import org.jiuling.network.tools.http.request.model.HttpRequestParam;
import org.jiuling.network.tools.http.request.model.HttpResponse;
import org.jiuling.network.tools.response.enums.HttpResponseStatus;
import org.jiuling.weather.moji.exception.MojiException;
import org.jiuling.weather.moji.exception.MojiRuntimeException;
import org.jiuling.weather.moji.model.AlertModel;
import org.jiuling.weather.moji.model.ForecastWeatherModel;
import org.jiuling.weather.moji.model.HourlyWeatherModel;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * 墨迹天气
 * @author 紫灵
 * @date 2022年09月14日 15时18分01秒
 */
public class MojiWeather {

    /** api域名列表 */
    private final List<String> apiDomainNameList = new ArrayList<>();
    /** 墨迹天气 */
    private org.jiuling.weather.moji.util.MojiWeather mojiWeather = new org.jiuling.weather.moji.util.MojiWeather();
    /** 阿里云市场配置 */
    private final AliyunMarketConfig aliyunMarketConfig;

    public MojiWeather(AliyunMarketConfig aliyunMarketConfig) {
        apiDomainNameList.add("http://aliv18.data.moji.com");
        apiDomainNameList.add("https://finacityid.market.alicloudapi.com");
        apiDomainNameList.add("https://aliv18.mojicb.com");
        this.aliyunMarketConfig = aliyunMarketConfig;
    }

    /**
     * 未来24小时
     * @param cityId 城市id
     * @return 小时天气模型列表
     */
    public List<HourlyWeatherModel> future24Hour(String cityId)  {
        try {
            return mojiWeather.future24Hour(requestDate(ApiEnums.FUTURE_24_HOUR, cityId));
        } catch (LocalToolsException | MojiException | IOException | NoSuchAlgorithmException | RequestException |
                 KeyManagementException e) {
            throw new MojiRuntimeException(e.getLocalizedMessage());
        }
    }

    /**
     * 未来15天
     * @param cityId 城市id
     * @return 预测天气模型列表
     */
    public List<ForecastWeatherModel> future15Day(String cityId){
        try {
            return mojiWeather.future15Day(requestDate(ApiEnums.FUTURE_15_DAY, cityId));
        } catch (LocalToolsException | MojiException | IOException | NoSuchAlgorithmException | RequestException |
                 KeyManagementException e) {
            throw new MojiRuntimeException(e.getLocalizedMessage());
        }
    }

    /**
     * 气象预警
     * @param cityId 城市id
     * @return 气象预警列表
     */
    public List<AlertModel> rainAlarmPro(String cityId) {
        try {
            return mojiWeather.rainAlarmPro(requestDate(ApiEnums.RAIN_ALARM_PRO, cityId));
        } catch (MojiException | IOException | LocalToolsException | NoSuchAlgorithmException | RequestException |
                 KeyManagementException e) {
            throw new MojiRuntimeException(e.getLocalizedMessage());
        }
    }

    /**
     * 请求数据
     * @param apiEnums API枚举
     * @param cityId 城市id
     * @return 返回的数据
     */
    private String requestDate(ApiEnums apiEnums,String cityId) throws IOException, LocalToolsException, NoSuchAlgorithmException, RequestException, KeyManagementException {
        Assertions.stringIsNotBlank("城市id不能为空！",cityId);
        String host = RandomValue.randomListData(apiDomainNameList);
        String path = apiEnums.getPath();
        String token = apiEnums.getToken();
        String appcode = aliyunMarketConfig.getAppCode();
        HttpRequest httpRequest = new HttpRequest();
        HttpRequestFactory httpRequestFactory = httpRequest.openHttpRequestFactory(host+path);
        HttpRequestHeader httpRequestHeader = new HttpRequestHeader()
                .add("Authorization","APPCODE "+appcode)
                .add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        HttpRequestParam httpRequestBody = new HttpRequestParam().add("cityId", cityId).add("token",token);
        httpRequestFactory.setRequestParam(httpRequestBody);
        httpRequestFactory.setRequestHeader(httpRequestHeader);
        httpRequestFactory.setRequestMethod(RequestMethodEnum.POST);
        HttpResponse httpResponse = httpRequest.send();
        // 判断请求返回数据是是否为200
        if(!HttpResponseStatus.OK.equals(httpResponse.getHttpResponseStatus())){
            throw new RequestException(httpResponse.getErrorBody());
        }
        return httpResponse.getBody();
    }

}
